import type {Directive} from "vue";
import router from "@/router";
import type {LoginSubmitResponsePermissionTreeDTOListType} from "@/types/loginType.ts";
import Local from "@/utils/local";
import {getTreeItem} from "@/utils";

const more: Directive<HTMLElement,  {
    list: string[], // has的数组
    len: number // 更多有几条数据
}> = (el, binding) => {
    const {list, len} = binding.value
    if (list.length === len) {
        const menuName: string = router.currentRoute.value.name as string;
        const menu:LoginSubmitResponsePermissionTreeDTOListType[] = JSON.parse(Local.localRead('menuList'))
        const row = getTreeItem<LoginSubmitResponsePermissionTreeDTOListType, string>(menu, menuName, 'componentName', 'children')
        if (row?.children){
            let num = 0
            // 显示
            for (const str of list) {
                const item = row?.children.find(item => item.status === '1' && item.perms === str)
                if (!item) {
                    num++
                }
            }

            if (num === list.length) {
                el.style.display = 'none' //不显示
            }
        } else {
            el.style.display = 'none' //不显示
        }
    }
}

export default more